using App.Untiys;
using System;
using System.IO;
using static System.Net.Mime.MediaTypeNames;

namespace App.Desk.CameraHelper.HK
{
	public class HK_IPCamera
	{
		private int m_lUserID;

		private int m_lRealHandle;

		public HK_IPCamera(string DVRIPAddress, int DVRPortNumber, string string_0, string string_1,out string msg)
		{
			this.m_lUserID = -1;
			this.m_lRealHandle = -1;
			msg = "";
            CHCNetSDK.NET_DVR_Init();
			if (this.m_lUserID < 0)
			{
				CHCNetSDK.NET_DVR_DEVICEINFO_V30 nET_DVR_DEVICEINFO_V = default(CHCNetSDK.NET_DVR_DEVICEINFO_V30);
				this.m_lUserID = CHCNetSDK.NET_DVR_Login_V30(DVRIPAddress, DVRPortNumber, string_0, string_1, ref nET_DVR_DEVICEINFO_V);
				if (this.m_lUserID < 0)
				{
					uint num = CHCNetSDK.NET_DVR_GetLastError();
					string text = "NET_DVR_Login_V30 failed, error code= " + num;
                    LogHelper.DoWriteLogType(Untiys.LogType.Camera, text);
                    LogHelper.DoWriteLogType(Untiys.LogType.Camera, "截图摄像头登录失败,【error code=" + num + "】");
					msg = "截图摄像头登录失败,【error code=" + num + "】";
                }
				else if (this.m_lRealHandle < 0)
				{
					CHCNetSDK.NET_DVR_PREVIEWINFO nET_DVR_PREVIEWINFO = default(CHCNetSDK.NET_DVR_PREVIEWINFO);
					nET_DVR_PREVIEWINFO.hPlayWnd = IntPtr.Zero;
					nET_DVR_PREVIEWINFO.lChannel = 1;
					nET_DVR_PREVIEWINFO.dwStreamType = 0u;
					nET_DVR_PREVIEWINFO.dwLinkMode = 0u;
					nET_DVR_PREVIEWINFO.bBlocked = true;
					IntPtr pUser = (IntPtr)0;
					this.m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(this.m_lUserID, ref nET_DVR_PREVIEWINFO, null, pUser);
					if (this.m_lRealHandle < 0)
					{
						uint num = CHCNetSDK.NET_DVR_GetLastError();
						string text = "NET_DVR_RealPlay_V40 failed, error code= " + num;
                        LogHelper.DoWriteLogType(Untiys.LogType.Camera, "截图摄像头打开预览失败！" + text);
                        LogHelper.DoWriteLogType(Untiys.LogType.Camera, "截图摄像头打开预览失败,【error code=" + num + "】");
                        msg = "截图摄像头打开预览失败,【error code=" + num + "】";
                    }
					else
                    {
                        LogHelper.DoWriteLogType(Untiys.LogType.Camera, "截图摄像头登录正常");
                        msg = "正常";
                    }
				}
			}
		}

		public bool CapturePic(out MemoryStream ms)
		{
			bool result;
			try
			{
				CHCNetSDK.NET_DVR_JPEGPARA nET_DVR_JPEGPARA = default(CHCNetSDK.NET_DVR_JPEGPARA);
				nET_DVR_JPEGPARA.wPicQuality = 0;
				nET_DVR_JPEGPARA.wPicSize = 255;
				result = CHCNetSDK.CaptureJPEGPicture(this.m_lUserID, 1, ref nET_DVR_JPEGPARA, out ms);
			}
			catch (Exception ex)
			{
                LogHelper.DoWriteLogType(Untiys.LogType.Camera, "抓图异常,【error code=" + ex.ToString() + "】");
				ms = null;
				result = false;
			}
			return result;
		}

        public bool SaveJPEG(string sJpegPicFileName)
        {
            CHCNetSDK.NET_DVR_JPEGPARA nET_DVR_JPEGPARA = default(CHCNetSDK.NET_DVR_JPEGPARA);
            nET_DVR_JPEGPARA.wPicQuality = 0;
            nET_DVR_JPEGPARA.wPicSize = 255;
            
           // string sPicFileName = "filetest.jpg";
            if (!CHCNetSDK.NET_DVR_CaptureJPEGPicture(m_lUserID, 1, ref nET_DVR_JPEGPARA, sJpegPicFileName))
            {
                LogHelper.DoWriteLogType(Untiys.LogType.Camera, "保存采样点位截图失败" + CHCNetSDK.NET_DVR_GetLastError());
                return false;
            }
            return true;
        }

    }
}
